# 查找Doxygen
find_package(Doxygen REQUIRED)
if(NOT DOXYGEN_FOUND)
    message(FATAL_ERROR "Doxygen is required to build documentation")
endif()

# 配置Doxygen
set(DOXYGEN_INPUT_DIR ${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/src)
set(DOXYGEN_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/doxygen)
set(DOXYGEN_INDEX_FILE ${DOXYGEN_OUTPUT_DIR}/html/index.html)
set(DOXYFILE_IN ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in)
set(DOXYFILE_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)

# 替换Doxyfile中的变量
configure_file(${DOXYFILE_IN} ${DOXYFILE_OUT} @ONLY)

# 获取所有头文件和源文件
file(GLOB_RECURSE DOXYGEN_INPUT_FILES 
    ${CMAKE_SOURCE_DIR}/include/*.h 
    ${CMAKE_SOURCE_DIR}/src/*.cpp
    ${CMAKE_SOURCE_DIR}/src/*.h
)

# 添加自定义命令来运行Doxygen
add_custom_command(
    OUTPUT ${DOXYGEN_INDEX_FILE}
    DEPENDS ${DOXYGEN_INPUT_FILES} ${DOXYFILE_OUT}
    COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYFILE_OUT}
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    COMMENT "Generating API documentation with Doxygen"
    VERBATIM
)

# 添加文档生成目标
add_custom_target(docs
    DEPENDS ${DOXYGEN_INDEX_FILE}
)

# 添加文档清理目标
add_custom_target(clean_docs
    COMMAND ${CMAKE_COMMAND} -E remove_directory ${DOXYGEN_OUTPUT_DIR}
    COMMENT "Cleaning documentation"
)

# 安装文档
install(DIRECTORY ${DOXYGEN_OUTPUT_DIR}/html
    DESTINATION share/doc/hft_system
)

# 安装PDF文档
install(FILES
    "${CMAKE_CURRENT_SOURCE_DIR}/CTP客户端开发指南 V3.5-中文.pdf"
    DESTINATION share/doc/hft_system
)

# 安装Markdown文档
install(FILES
    execution.md
    market_data.md
    monitor.md
    risk.md
    strategy.md
    hft_system_code_analysis.md
    DESTINATION share/doc/hft_system
)